package com.wontube.smr.web.controller.video;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.web.client.IFeaturesHandler;
import com.wontube.smr.web.client.model.bo.QueryItemParams;

/**
 * more video
 * 
 * @author tanggf(2012-4-24)
 * 
 */
@Controller
public class FeaturedVideoController {
	private final static int PAGE_SIZE = 30;
	private final static String TODAY_PERIOD = "today";
	
	@RequestMapping("/featured")
	public void f1(HttpServletRequest request, 
			HttpServletResponse response) throws IOException{	
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		response.sendRedirect(request.getContextPath() + "/featured/");	
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping("/featured/")
	public ModelAndView featuredHome() {
		return youtubeFeatureds();
	}
	
	@RequestMapping("/featured/youtube")
	public void f2(HttpServletRequest request, 
			HttpServletResponse response) throws IOException{	
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		response.sendRedirect(request.getContextPath() + "/featured/youtube/");	
	}

	@RequestMapping("/featured/youtube/")
	public ModelAndView youtubeFeatureds() {
		ModelAndView mv = listYoutubeMostVieweds(null, null, null, true);
		mv.setViewName("video/featured/youtube/youtube_index");
		return mv;
	}
	
	@RequestMapping("/featured/youtube/query")
	public ModelAndView listYoutubes(@RequestParam(value="period", required=false) String period, 
			@RequestParam(value="genre", required=false) String genre, 
			@RequestParam(value="page", required=false) Integer page){
		ModelAndView mv = listYoutubeMostVieweds(period, genre, page, false);
		mv.setViewName("video/featured/youtube/youtube_list");	
		return mv;
	}
	
	@RequestMapping("/featured/youtube/list/{page}")
	public ModelAndView listYoutubeVideos(@PathVariable("page") Integer page){
		ModelAndView mv = listYoutubeMostVieweds(null, null, page, true);
		mv.setViewName("video/featured/youtube/youtube_index");
		return mv;
	}
	
	private ModelAndView listYoutubeMostVieweds(String period, String genre, Integer page, boolean needGenres){
		if(page == null || page.intValue() < 1){
			page = 1;
		}		
		if(period == null){
			period = TODAY_PERIOD;
		}
		QueryItemParams params = new QueryItemParams();
		params.setGenre(genre);
		params.setPeriod(period);
		params.setNeedGenres(needGenres);
		
		IFeaturesHandler handler = getIFeaturesHandler();
		MovieItems movieItems = handler.listYoutubeMostViewed(params, new PagenateInfo(page, PAGE_SIZE));
		ModelAndView mv = new ModelAndView();
		if(needGenres && movieItems != null){
			mv.addObject("genres", movieItems.getGenres());
		}
		mv.addObject("movieItems", movieItems);
		mv.addObject("page", page);
		mv.addObject("period", period);
		return mv;
	}
	
	@RequestMapping("/featured/youtube/{id}")
	public ModelAndView youtubeDetail(@PathVariable("id") String id,
			HttpServletResponse response) throws IOException {
		IFeaturesHandler handler = getIFeaturesHandler();
		Movie movie = handler.getShortFilm(id);
		if (movie == null) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return null;
		} else {
			ModelAndView mv = new ModelAndView("video/featured/youtube/detail/youtube_detail_index");
			mv.addObject("show", movie);
			return mv;
		}		
	}
	
	@RequestMapping("/featured/youtube/watch-{id}")
	public ModelAndView playYoutube(@PathVariable("id") String id,
			HttpServletResponse response) throws IOException {
		IFeaturesHandler handler = getIFeaturesHandler();
		Movie movie = handler.getShortFilm(id);
		if (movie == null) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return null;
		} else {
			ModelAndView mv = new ModelAndView("video/featured/youtube/play/youtube_play");
			mv.addObject("show", movie);
			return mv;
		}		
	}
	
	@RequestMapping("/featured/youtube/relate/{id}")
	public ModelAndView youtubeRelated(@PathVariable("id") String id) {
		IFeaturesHandler handler = getIFeaturesHandler();
		MovieItems items = handler.listRelated(id, new PagenateInfo(1, 5));
		ModelAndView mv = new ModelAndView("video/featured/youtube/play/youtube_relate_video");
		mv.addObject("movieItems", items);
		return mv;
	}
		
	private IFeaturesHandler getIFeaturesHandler() {
		Remoting<IFeaturesHandler> remoting = new Remoting<IFeaturesHandler>();
		return remoting.createService(IFeaturesHandler.class);
	}
}
